package com.sinosoft.system.primary.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sinosoft.system.primary.biz.SysRoleMenuBiz;
import com.sinosoft.system.primary.entity.SysRoleMenu;
import com.sinosoft.system.util.Result;
import com.sinosoft.system.util.page.Pagenation;

/*
 * 
 * GET 方式请求 /SysRoleMenu/selectAll/ 			返回数据列表
 * GET 方式请求 /SysRoleMenu/select/{id}	  			查询指定ID的数据，返回一条数据
 * POST方式请求 /SysRoleMenu/select		 			分页查询数据列表
 * POST方式请求 /SysRoleMenu/insert               	新增一条数据
 * POST方式请求 /SysRoleMenu/update		          	更新指定ID的数据
 * GET 方式请求 /SysRoleMenu/delete/{id}          	删除指定ID的数据
 * GET 方式请求 /SysRoleMenu/delete  				 删除所以数据
 * 
 * @author shangxp
 * @date 2017-1-8
 * 
 */


@RestController
@RequestMapping("SysRoleMenu") 
public class SysRoleMenuAction {

	private static Logger log = Logger.getLogger(SysRoleMenuAction.class);
	
	@Autowired
	private SysRoleMenuBiz sysRoleMenuBiz;

	@RequestMapping(value = "/selectAll", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.POST}) 
    public ResponseEntity<List<SysRoleMenu>> selectAllSysRoleMenu() { 
     
		log.info("查询所有的SysRoleMenu数据");
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuBiz.selectAllSysRoleMenu();
        if(sysRoleMenus.isEmpty()){  
            return new ResponseEntity<List<SysRoleMenu>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<List<SysRoleMenu>>(sysRoleMenus, HttpStatus.OK);  
    }
	
	@RequestMapping(value = "/select/{id}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.GET}) 
    public ResponseEntity<SysRoleMenu> selectSysRoleMenuById(@PathVariable("id") int id) {  
		log.info("查询ID="+id+"的SysRoleMenu数据");
        SysRoleMenu sysRoleMenu = sysRoleMenuBiz.selectSysRoleMenu(id);
        if(sysRoleMenu==null){  
            return new ResponseEntity<SysRoleMenu>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<SysRoleMenu>(sysRoleMenu, HttpStatus.OK);  
    }
	
	@RequestMapping(value = "/select", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.POST})   
    public ResponseEntity<Pagenation> selectPageSysRoleMenu(HttpServletRequest request) {  
		log.info("分页查询SysRoleMenu数据");
		//  分页查询开始
		int pageNumber = new Integer(request.getParameter("page"));//easyui传入的是page是页码
		int pageSize = new Integer(request.getParameter("rows"));//easyui传入的rows是页大小
		List<String> queryKeys = new ArrayList<String>();
		List<Object> queryValues = new ArrayList<Object>();
		//添加查询条件   根据查询情况修改
/*		String username = request.getParameter("username");
		if(username != null && !"".equals(username.trim())){
			queryKeys.add("username"); //querykey 对应 数据库 字段名，dao查询时拼SQL使用
			queryValues.add(username); //queryValue  对应查询值
		}
*/		
		Pagenation page = new Pagenation(pageSize,pageNumber,queryKeys,queryValues);
		page = sysRoleMenuBiz.selectPageSysRoleMenu(page);
		
        if(page==null){  
            return new ResponseEntity<Pagenation>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<Pagenation>(page, HttpStatus.OK);  
    }
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.POST}) 
    public ResponseEntity<Result> insertSysRoleMenu(SysRoleMenu sysRoleMenu) {
		log.info("插入一条SysRoleMenu数据："+sysRoleMenu.toString());
		
		if(sysRoleMenuBiz.insertSysRoleMenu(sysRoleMenu)!=1){
			return new ResponseEntity<Result>(new Result("insertError","新增数据失败"), HttpStatus.OK); 
		}else{
			return new ResponseEntity<Result>(new Result("insertOk","新增数据成功"), HttpStatus.OK);
		}
    }
	
	@RequestMapping(value = "/update", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.POST})  
    public ResponseEntity<Result> updateSysRoleMenu(SysRoleMenu sysRoleMenu) {  
		log.info("更新一条SysRoleMenu数据："+sysRoleMenu.toString());		
		if(sysRoleMenuBiz.updateSysRoleMenu(sysRoleMenu)!=1){
			return new ResponseEntity<Result>(new Result("updateError","更新数据失败"), HttpStatus.OK); 
		}else{
			return new ResponseEntity<Result>(new Result("updateOk","更新数据成功"), HttpStatus.OK);   
		}
    }
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.GET})  
    public ResponseEntity<Result> deleteSysRoleMenuById(@PathVariable("id") int id) { 
		log.info("删除ID="+id+"的SysRoleMenu数据");
		if(sysRoleMenuBiz.deleteSysRoleMenu(id)!=1){
			return new ResponseEntity<Result>(new Result("deleteError","删除数据失败"), HttpStatus.OK); 
		}else{
			return new ResponseEntity<Result>(new Result("deleteOk","删除数据成功"), HttpStatus.OK);   
		}
    }
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.POST})  
    public ResponseEntity<String> deleteSysRoleMenu() {  
		log.info("删除所有的SysRoleMenu数据");		
		//为实现
        return new ResponseEntity<String>("OK", HttpStatus.OK);  
    }
	

	public SysRoleMenuBiz getSysRoleMenuBiz() {
		return sysRoleMenuBiz;
	}

	public void setSysRoleMenuBiz(SysRoleMenuBiz sysRoleMenuBiz) {
		this.sysRoleMenuBiz = sysRoleMenuBiz;
	}

}
